package com.xiaohudie.SGGJUC;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Author xiao hu die
 * @Date 2022/3/10 14:55
 * @Version 1.0
 */
public class TestExecutors {
    public static void main(String[] args) {
        /**线程池中只有一个线程*/
        ExecutorService ThreadPool = Executors.newSingleThreadExecutor();
        /**自定义线程池中线程数量*/
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        /**缓存线程数*/
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        try {
            for (int i = 0; i < 10; i++) {
                ThreadPool.execute(() -> {
                    System.out.println(Thread.currentThread().getName());
                });
            }
            for (int i = 0; i < 20; i++) {
                executorService.execute(() -> {
                    System.out.println(Thread.currentThread().getName());
                });
            }
            for (int i = 0; i < 30; i++) {
                executorService1.execute(() -> {
                    System.out.println(Thread.currentThread().getName());
                });
            }
        }finally {
            ThreadPool.shutdown();
            executorService.shutdown();
            executorService1.shutdown();
        }
    }
}
